package com.zqx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.*;
import com.zqx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class NormalUserController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private DormService dormService;
    @Autowired
    private MoveService moveService;
    @Autowired
    private FixService fixService;
    @Autowired
    private BoardService boardService;
    @Autowired
    private NoticeService noticeService;

    //查询所有学生
    @RequestMapping("listStudent")
    public String listStudent(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize,
                              String sname, Model model) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        QueryWrapper<Student> qw = new QueryWrapper<>();
        if (sname != null) {
            qw.like("sname", sname);
        }
        List<Student> list = studentService.list(qw);
        for (Student student1 : list) {
            if (student1.getDid() == null) {
                student1.setDorm("暂未分配宿舍");
            } else {
                Integer did = student1.getDid();
                Dorm dorm = dormService.getById(did);
                student1.setDorm(dorm.getBuild() + "-" + dorm.getDormid());
            }
        }
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "user-student-list";
    }

    //学生宿舍信息
    @RequestMapping("listDorm")
    public String listDorm(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize, Dorm dorm, Model model) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        QueryWrapper<Dorm> qw = new QueryWrapper<>();
        if (dorm.getBuild() != null) {
            qw.like("build", dorm.getBuild());
        }

        List<Dorm> list = dormService.list(qw);

        for (Dorm dorm1 : list) {
            if (dorm1.getSetting() >= dorm1.getPeople()) {
                dorm1.setStatus(1);
                dormService.updateById(dorm1);
            } else {
                dorm1.setStatus(0);
                dormService.updateById(dorm1);
            }

        }
        PageInfo<Dorm> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "user-dorm-list";
    }

    //查看学生的宿舍信息
    @RequestMapping("check/{id}")
    public String check(@PathVariable Integer id, Model model) {
        //条件构造器
        QueryWrapper<Student> qw = new QueryWrapper<>();
        //查询所传入的宿舍id与学生里面的字段did相同的学生
        qw.eq("did", id);
        List<Student> list = studentService.list(qw);
        model.addAttribute("studentList", list);
        return "user-student-check";
    }

    //学生迁移
    @RequestMapping("listMove")
    public String listMove(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize,
                           Student student, Model model, HttpSession httpSession) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        String currentUser = (String) httpSession.getAttribute("currentUser");
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Move> qw = new QueryWrapper<>();
        if (student.getSname() != null) {
            qw.like("sname", student.getSname());
        }
        qw.eq("sname", currentUser);
        List<Move> list = moveService.list(qw);
        PageInfo<Move> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        List<Dorm> list1 = dormService.list(null);
        model.addAttribute("dormList", list1);
        return "user-move-list";
    }

    //学生申请宿舍搬迁
    @RequestMapping("saveMove")
    public String saveMove(Integer did, HttpSession httpSession) {
        String currentUser = (String) httpSession.getAttribute("currentUser");
        Dorm byId = dormService.getById(did);
        if (byId.getStatus() == 1) {
            return "redirect:/listMove";
        }
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("sname", currentUser);
        Student one = studentService.getOne(qw);
        Move move = new Move();
        move.setSname(currentUser);
        move.setStatus(0);
        move.setMtime(new Date());
        move.setSimage(one.getSimage());
        move.setInid(did);
        move.setIndorm(byId.getBuild() + "-" + byId.getDormid());
        if (one.getDid() == null) {
            move.setOutdorm("暂未分配宿舍");
        } else {
            Dorm byId1 = dormService.getById(one.getDid());
            move.setOutdorm(byId1.getBuild() + "-" + byId1.getDormid());
        }
        boolean save = moveService.save(move);
        return "redirect:/listMove";
    }

    //删除学生搬迁申请
    @RequestMapping("delMove/{id}")
    public String delMove(@PathVariable Integer id) {
        boolean b = moveService.removeById(id);
        return "redirect:/listMove";
    }

    //当前学生保修记录
    @RequestMapping("listFix")
    public String listFix(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize,
                          Student student, Model model, HttpSession httpSession) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        String currentUser = (String) httpSession.getAttribute("currentUser");
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Fix> qw = new QueryWrapper<>();
        if (student.getSname() != null) {
            qw.like("sname", student.getSname());
        }
        qw.eq("sname", currentUser);
        List<Fix> list = fixService.list(qw);
        for (Fix fix : list) {
            Dorm byId = dormService.getById(fix.getDid());
            fix.setDorm(byId.getBuild() + "-" + byId.getDormid());
        }
        PageInfo<Fix> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "user-fix-list";
    }

    @RequestMapping("saveFix")
    public String saveFix(Fix fix, HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userId");
        Student byId = studentService.getById(userId);
        fix.setSname(byId.getSname());
        fix.setSimage(byId.getSimage());
        fix.setStatus(0);
        fix.setDid(byId.getDid());
        boolean save = fixService.save(fix);
        return "redirect:/listFix";
    }

    @RequestMapping("delFix/{id}")
    public String delFix(@PathVariable Integer id) {
        boolean b = fixService.removeById(id);
        return "redirect:/listFix";
    }

    @RequestMapping("listNotice")
    public String listNotice(Model model) {
        List<Notice> list = noticeService.list(null);
        model.addAttribute("noticeList", list);
        return "user-notice-list";
    }

    @RequestMapping("listBoard")
    public String listBoard(Model model) {
        List<Board> list = boardService.list(null);
        model.addAttribute("boardList", list);
        return "user-board-list";
    }

    @RequestMapping("saveBoard")
    public String saveBoard(Board board, HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userid");
        Student student = studentService.getById(userId);
        board.setSname(student.getSname());
        board.setEmail(student.getEmail());
        board.setSimage(student.getSimage());
        board.setBtime(new Date());
        boolean save = boardService.save(board);
        return "redirect:/listBoard";
    }
}
